<?php

namespace Admin\Controller;

use Api\Event\Cache\UserDailyTopEvent;

// +----------------------------------------------------------------------
// | 文件 UserTopController.class.php
// +----------------------------------------------------------------------
// | 说明 设置每日之星
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class UserDailyTopController extends BaseManageController 
{

    protected $modelName = 'UserDailyTop'; 
  
    protected $is_del   = false;

    // public function _before_index()
    // {
    //     parent::_before_index();
    //     $this->assign('types', ['auto', 'last', 'manual']);
    // }

    public function user($id = 0)
    {
        $id = (int)$id;
        $bang = $this->model->find($id);

        $auto_date = $bang['auto_date'] ?: date('Ymd', strtotime($bang['date'] . ' -1 day'));
        $topUsers = UserDailyTopEvent::get($auto_date, 10);
        if (empty($bang['uids'])){
            $bang['uids'] = $topUsers;
        }
        if (IS_POST){
            $save['uids'] = array_map('intval', I('uids'));
            $this->model->where(['id' => $id])->save($save);
            $this->success('操作成功');
        }else{
            $this->assign('bang', $bang);
            $this->assign('topUsers', $topUsers);
            $this->display();
        }
    }

    // 搜索用户
    public function search($nickname = '')
    {
        if (!$nickname) return [];
        $map['nickname'] = ['$regex' => new \MongoRegex("/^$nickname/")];
        $map['status'] = 1;
        $users = service('Users')->where($map)->select();
        // echo service('Users')->_sql();
        foreach ($users as $key => &$user) {
            $user['uid'] = $user['id'];
        }
        $this->ajaxReturn($users ?: []);
    }


}